package com.yanqu.road.server.cmd.activity.drink;

import com.yanqu.road.entity.activity.drink.DrinkUserData;
import com.yanqu.road.entity.activity.drink.config.DrinkConfig;
import com.yanqu.road.pb.activity.DrinkActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.drink.DrinkModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.drink.DrinkMgr;
import com.yanqu.road.server.pb.DrinkPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_DRINK_GET_GAME_DATA, desc = "获取游戏数据")
public class DrinkGetGameDataCmd extends AbstractCommand {

    private int multi;

    private DrinkActivityProto.DrinkGameDataMsg.Builder gameData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DrinkActivityProto.DrinkGetGameDataReqMsg reqMsg = DrinkActivityProto.DrinkGetGameDataReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.hasNewMulti()) {
            multi = reqMsg.getNewMulti();
        } else {
            multi = reqMsg.getMulti() ? 8 : 1;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        DrinkConfig drinkConfig = DrinkMgr.getDrinkConfig();
        if (drinkConfig == null || !ActivityMgr.activityInShowTime(drinkConfig.getActivityInfo())) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_OPEN;
            return;
        }

        // 旧配置的活动不能用16倍
        if (multi == 16 && drinkConfig.getFastUnlockVipLevel() == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_UNLOCK_MULTI;
            return;
        }

        DrinkModule module = player.getModule(DrinkModule.class);
        DrinkUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_UNLOCK;
            return;
        }

        gameData = DrinkPb.parseDrinkGameDataMsg(userData, multi);
    }

    @Override
    public void sendData(GamePlayer player) {
        DrinkActivityProto.DrinkGetGameDataRespMsg.Builder builder = DrinkActivityProto.DrinkGetGameDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setGameData(gameData);
        }
        player.sendPacket(ClientProtocol.U_DRINK_GET_GAME_DATA, builder);
    }
}
